package dk.modified.radioonlineapp;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;

public class ByteConverter {
	
	// Takes a URL pointing to a .png file, and returns the .png as a bitmap.
	public static Bitmap getBitmapFromURL(String url1, Bitmap defaultImg) {
		try {

			Bitmap bmp = null;

			URL url = new URL(url1);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setDoInput(true);
			connection.connect();
			
			InputStream input = connection.getInputStream();
			bmp = BitmapFactory.decodeStream(input);
			return bmp;
		} catch (IOException e) {
			e.printStackTrace();
			return defaultImg;
		}
	}
	
	public static Bitmap ConvertToBitmap(int resource, Context context){
		Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
                resource);
		return icon;
	}
	
	// Takes a string and converts it to an encrypted MD5
	public static String MD5(String md5) {
		try {
			java.security.MessageDigest md = java.security.MessageDigest
					.getInstance("MD5");
			byte[] array = md.digest(md5.getBytes());
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < array.length; ++i) {
				sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100)
						.substring(1, 3));
			}
			return sb.toString();
		} catch (java.security.NoSuchAlgorithmException e) {
		}
		return null;
	}
	
	// This method has two functions, one is to plus a radio-listen count by one on the server this is if "boolean countUp == true"
	// If count up is false, it posts an error message to the server, collecting a lot of useful information from the mobile device.
	public static void postData(String s, int id, boolean countUp) {
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = null;
		if (countUp == true) {
			httppost = new HttpPost(
					"https://itunedradio.com/app_access/count_up.php");
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
			nameValuePairs
					.add(new BasicNameValuePair("id", String.valueOf(id)));
			nameValuePairs.add(new BasicNameValuePair("chk", s));
			try {
				httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//				HttpResponse response = httpclient.execute(httppost);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			String androidOS = Build.VERSION.RELEASE;
			String board = Build.BOARD;
			String bootloader = Build.BOOTLOADER;
			String brand = Build.BRAND;
			String device = Build.DEVICE;
			String display = Build.DISPLAY;
			String fingerprint = Build.FINGERPRINT;
			String hardware = Build.HARDWARE;
			String host = Build.HOST;
			String androidId = Build.ID;
			String manufacturer = Build.MANUFACTURER;
			String model = Build.MODEL;
			String product = Build.PRODUCT;
			String type = Build.TYPE;
			String user = Build.USER;
			httppost = new HttpPost(
					"http://radioonline.dk/app_access/error_callback.php");
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
			nameValuePairs.add(new BasicNameValuePair("androidOS", androidOS));
			nameValuePairs.add(new BasicNameValuePair("versioncode", s));
			nameValuePairs
					.add(new BasicNameValuePair("id", String.valueOf(id)));
			nameValuePairs.add(new BasicNameValuePair("board", board));
			nameValuePairs
					.add(new BasicNameValuePair("bootloader", bootloader));
			nameValuePairs.add(new BasicNameValuePair("brand", brand));
			nameValuePairs.add(new BasicNameValuePair("device", device));
			nameValuePairs.add(new BasicNameValuePair("display", display));
			nameValuePairs.add(new BasicNameValuePair("fingerprint",
					fingerprint));
			nameValuePairs.add(new BasicNameValuePair("hardware", hardware));
			nameValuePairs.add(new BasicNameValuePair("host", host));
			nameValuePairs.add(new BasicNameValuePair("androidID", androidId));
			nameValuePairs.add(new BasicNameValuePair("hardware", hardware));
			nameValuePairs.add(new BasicNameValuePair("manufacturer",
					manufacturer));
			nameValuePairs.add(new BasicNameValuePair("model", model));
			nameValuePairs.add(new BasicNameValuePair("product", product));
			nameValuePairs.add(new BasicNameValuePair("type", type));
			nameValuePairs.add(new BasicNameValuePair("user", user));
			try {
				httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//				HttpResponse response = httpclient.execute(httppost);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	// Takes a string, spits it out as byte[].
	public static byte[] convertStringToByteArr(String data) {
		return data.getBytes();
	}
}
